package cn.ecnu.utils;

import cn.ecnu.dao.OrderDao;
import cn.ecnu.dao.RecordDao;
import cn.ecnu.entity.*;
import cn.ecnu.feign.ManagementFeignService;
import cn.ecnu.vo.OrderVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class Converter {

    @Resource
    private RecordDao recordDao;

    @Resource
    private ManagementFeignService managementFeignService;

    @Resource
    private ObjectMapper objectMapper;

    public OrderVo OrderToVo(Order order) {
        if (order == null) {
            return null;
        }
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(order, orderVo);
        if (order.getCid() != null)
            orderVo.setCar(objectMapper.convertValue(managementFeignService.getCarById(order.getCid()).get("data"), Car.class));
        if (order.getEid() != null)
            orderVo.setEmployee(objectMapper.convertValue(managementFeignService.getEmployeeById(order.getEid()).get("data"), Employee.class));
        orderVo.setReceiveAddress(objectMapper.convertValue(managementFeignService.getAddressById(order.getReceiveAddressId()).get("data"), Address.class));
        orderVo.setSendAddress(objectMapper.convertValue(managementFeignService.getAddressById(order.getSendAddressId()).get("data"), Address.class));
        // 订单状态
        Record[] records = recordDao.getRecordsByOid(order.getId());
        orderVo.setRecords(records);
        return orderVo;
    }
}
